package com.base.cn.platform.os.service.op.article;

import com.base.cn.platform.os.common.mybatis.BaseBiz;
import com.base.cn.platform.os.common.utils.DataUtil;
import com.base.cn.platform.os.dao.op.article.OpArticleContextDao;
import com.base.cn.platform.os.entity.op.article.OpArticleContext;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.List;
import java.util.Map;

@Service
public class OpArticleContextBiz extends BaseBiz<OpArticleContext,OpArticleContextDao> {


    /**
     * 根据资讯id串查询资讯内容
     * @param articleIds 资讯id串
     * @return
     */
    public Map<BigDecimal, OpArticleContext> findArticleContextByArticleIds(String articleIds) {
        String where = " articleId in (" + articleIds + ")";
        List<OpArticleContext> articleContextList = find(where, null, null);
        return DataUtil.toMap(articleContextList, OpArticleContext::getArticleId);
    }

    /**
     * 保存资讯内容
     * @param articleContext 资讯内容
     * @return
     */
    public OpArticleContext saveArticleContext(OpArticleContext articleContext) {
        if (DataUtil.idIsNotNull(articleContext.getArticleId())) {
            BigDecimal articleId = articleContext.getArticleId();
            Map<BigDecimal, OpArticleContext> articleContextMap = findArticleContextByArticleIds(articleId.toString());
            OpArticleContext database = articleContextMap.get(articleContext.getArticleId());
            if (database != null) {
                database.setContext(articleContext.getContext());
                updateById(database, null);
                return database;
            }
        }
        save(articleContext);
        return articleContext;
    }

}
